package ru.spb.osll.myfit.common;

import com.google.gwt.user.client.ui.Anchor;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import ru.spb.osll.myfit.client.view.ViewConstants;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@XStreamAlias("suiteExecutionResult")
public class SuiteExecutingResult extends ExecutingResult {
  private static final long serialVersionUID = -7502924453436685057L;

  @XStreamAlias("progressCounts")
  private SuiteProgressCounts m_counts = new SuiteProgressCounts();

  @XStreamAlias("pageExecutionResults")
  private Set<String> m_pageExecutingResults = new LinkedHashSet<String>();


  public void reset() {
    m_counts.reset();
    m_pageExecutingResults.clear();
  }

  public SuiteExecutingResult() {}

  public SuiteExecutingResult(SuiteExecutingResult result) {
    m_pageExecutingResults.addAll(result.getPageExecutingResults());
    m_counts.setCounts(result.getCounts());
    m_id = result.getId();
    m_state = result.getState();
  }
  
  public SuiteExecutingResult(SuiteProgressCounts counts, String id, ExecutingState state, String name, Date startTime) {
    m_counts.setCounts(counts);
    m_id = id;
    m_state = state;
    m_name = name;
    m_startTime = startTime;
  }

  public SuiteProgressCounts getCounts() {
    return m_counts;
  }

  public void addPageExecutingResult(PageExecutingResult result) {
    m_pageExecutingResults.add(result.getId());
  }

  public Set<String> getPageExecutingResults() {
    return m_pageExecutingResults;
  }

  @Override
  public String getStatusString() {
    int progress = m_counts.getProgress();
    int passed = m_counts.getPassed();
    int total = m_counts.getTotal();
    int failed = m_counts.getFailed();

    return "Processed: " + progress + ", passed: " + passed +
            ", failed: " + failed + " out of " + total;
  }

  @Override
  public boolean isWrong() {
    return m_counts.getFailed() > 0;
  }

  @Override
  public Anchor getLink() {
    Anchor link = new Anchor(getName(), ViewConstants.SUITE_RESULT_ADDRESS + "?id=" + m_id);
    link.setStyleName("suiteResultLink");
    return link;
  }

  @Override
  public double getPercentage() {
    return m_counts.getPercentage();
  }

  @Override
  public List<Table> getExecutedTables() {
    return null;
  }
}
